package leetcode_1100;

/**
 *@author 周杨
 *MinimumDominoRotationsForEqualRow_1007 给两个数组 数组里只出现1-6 这种数字 问最少能翻转几次 只能交换A与B同index的元素 使两个数组里的元素一致
 *describe:常量记录就可 只有四种情况
 *2019年3月23日 下午13:32
 */
public class MinimumDominoRotationsForEqualRow_1007 {
	public static void main(String[] args) {
		MinimumDominoRotationsForEqualRow_1007 test = new MinimumDominoRotationsForEqualRow_1007();
		System.out.println(test.minDominoRotations(new int[] {1,2,1,1,1,2,2,2},new int[] {2,1,2,2,2,2,2,2}));
	}
	
	public int minDominoRotations(int[] A, int[] B) {
        int may1 = A[0],may2 = B[0];
        int res1 = 0,res2 = 1,res3 =0 ,res4 =1;
        boolean flag1 = true,flag2 =true;//换成元素2
        for(int i = 1; i < A.length; ++i) {
        	if(A[i] != may1 && B[i] != may1) flag1 = false;
        	if(A[i] != may2 && B[i] != may2) flag2 = false;
        	if(flag1) {//元素1仍有可能
        		if(A[i] == may1 && B[i] != may1) {
        			++res4;
        		}
        		if(B[i] == may1 && A[i] != may1) {
        			++res1;
        		}
        	}
        	if(flag2) {
        		if(A[i] == may2 && B[i] != may2) {
        			++res3;
        		}
        		if(B[i] == may2 && A[i] != may2) {
        			++res2;
        		}
        	}else {
        		return -1;
        	}
        }
        int res = Integer.MAX_VALUE;
        if(flag1) res = Math.min(res1, res4);
        if(flag2) res = Math.min(res,Math.min(res2, res3));
        return res;
    }
}
